﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuanLyPhongGiam
{
    public partial class frmPhucHoi : Form
    {
        public frmPhucHoi()
        {
            InitializeComponent();
        }
        SqlConnection con = new SqlConnection();
        private void ketnoi()
        {
            string kn = "Data Source=(local);Initial Catalog=QuanLySinhVien;Integrated Security=True";
            try
            {
                con = new SqlConnection(kn);
                con.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("không thể kết nối tới CSDL!", "Thông Báo !", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void huyketnoi()
        {
            con.Close();
            con.Dispose();//giai fong tai nguyen
            con = null;//huy doi tương
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void btnDuongDanSaoLuu_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.FileName = "";
            open.Filter = "File(*.bak)|*.bak";
            if (open.ShowDialog() == DialogResult.OK)
            {
                txtPhucHoi.Text = open.FileName;
            }
        }

        private void btnSaoLuu_Click(object sender, EventArgs e)
        {
            ketnoi();
            string phuchoi = " USE MASTER restore database QuanLySinhVien from disk = '" + txtPhucHoi.Text + "' ";
            SqlCommand cmd = new SqlCommand(phuchoi, con);
            cmd.ExecuteNonQuery();
            MessageBox.Show("Phục hồi thành công !");
            huyketnoi();
        }
    }
}
